package com.ruoyi.tb.lmt.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.tb.lmt.domain.bo.McgsQj1QdjczBo;
import com.ruoyi.tb.lmt.domain.vo.McgsQj1QdjczVo;
import com.ruoyi.tb.lmt.domain.McgsQj1Qdjcz;
import com.ruoyi.tb.lmt.mapper.McgsQj1QdjczMapper;
import com.ruoyi.tb.lmt.service.IMcgsQj1QdjczService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 制动阀试验台_QJ1气动集成组_MCGSService业务层处理
 *
 * @author ruoyi
 * @date 2023-12-13
 */
@RequiredArgsConstructor
@Service
public class McgsQj1QdjczServiceImpl implements IMcgsQj1QdjczService {

    private final McgsQj1QdjczMapper baseMapper;

    /**
     * 查询制动阀试验台_QJ1气动集成组_MCGS
     */
    @Override
    public McgsQj1QdjczVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询制动阀试验台_QJ1气动集成组_MCGS列表
     */
    @Override
    public TableDataInfo<McgsQj1QdjczVo> queryPageList(McgsQj1QdjczBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<McgsQj1Qdjcz> lqw = buildQueryWrapper(bo);
        Page<McgsQj1QdjczVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询制动阀试验台_QJ1气动集成组_MCGS列表
     */
    @Override
    public List<McgsQj1QdjczVo> queryList(McgsQj1QdjczBo bo) {
        LambdaQueryWrapper<McgsQj1Qdjcz> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<McgsQj1Qdjcz> buildQueryWrapper(McgsQj1QdjczBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<McgsQj1Qdjcz> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, McgsQj1Qdjcz::getId, bo.getId());
        lqw.eq(StringUtils.isNotBlank(bo.getDjNo()), McgsQj1Qdjcz::getDjNo, bo.getDjNo());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTime()), McgsQj1Qdjcz::getMcgsTime, bo.getMcgsTime());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsTimeMs()), McgsQj1Qdjcz::getMcgsTimeMs, bo.getMcgsTimeMs());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsJxbh()), McgsQj1Qdjcz::getMcgsJxbh, bo.getMcgsJxbh());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsCzz()), McgsQj1Qdjcz::getMcgsCzz, bo.getMcgsCzz());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsZsch()), McgsQj1Qdjcz::getMcgsZsch, bo.getMcgsZsch());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsQj11()), McgsQj1Qdjcz::getMcgsQj11, bo.getMcgsQj11());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsQj12()), McgsQj1Qdjcz::getMcgsQj12, bo.getMcgsQj12());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsQj13()), McgsQj1Qdjcz::getMcgsQj13, bo.getMcgsQj13());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsQj14()), McgsQj1Qdjcz::getMcgsQj14, bo.getMcgsQj14());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsQj15()), McgsQj1Qdjcz::getMcgsQj15, bo.getMcgsQj15());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsSch()), McgsQj1Qdjcz::getMcgsSch, bo.getMcgsSch());
        lqw.eq(StringUtils.isNotBlank(bo.getMcgsBy1()), McgsQj1Qdjcz::getMcgsBy1, bo.getMcgsBy1());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), McgsQj1Qdjcz::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), McgsQj1Qdjcz::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, McgsQj1Qdjcz::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), McgsQj1Qdjcz::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, McgsQj1Qdjcz::getUpdateTime, bo.getUpdateTime());
        return lqw;
    }

    /**
     * 新增制动阀试验台_QJ1气动集成组_MCGS
     */
    @Override
    public Boolean insertByBo(McgsQj1QdjczBo bo) {
        McgsQj1Qdjcz add = BeanUtil.toBean(bo, McgsQj1Qdjcz.class);
        add.setDelFlag("0");
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改制动阀试验台_QJ1气动集成组_MCGS
     */
    @Override
    public Boolean updateByBo(McgsQj1QdjczBo bo) {
        McgsQj1Qdjcz update = BeanUtil.toBean(bo, McgsQj1Qdjcz.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(McgsQj1Qdjcz entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除制动阀试验台_QJ1气动集成组_MCGS
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public String getMaxMcgsTime(){
        return baseMapper.getMaxMcgsTime();
    }
}
